package com.yingside.jdbc.gui;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.yingside.jdbc.Teacher;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class TeacherMainFrame extends JFrame {

    private LovoTable table;
    private LovoButton btnDel;
    private TeacherDao teacherDao = new TeacherDao();

    public TeacherMainFrame(){
        this.setTitle("老师管理界面");
        this.setBounds(200,100,500,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);

        initTable();
        initData();
        initButton();
    }
    public void initButton(){
        btnDel = new LovoButton("删除",30,230,this);
        btnDel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int key = table.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选中一行再进行删除操作");
                    return;
                }

                int m = JOptionPane.showConfirmDialog(null,"你确定要删除吗?","删除提示",JOptionPane.OK_CANCEL_OPTION);

                if(m == 0){
                    int delete = teacherDao.delete(key);
                    if(delete > 0){
                        JOptionPane.showMessageDialog(null,"删除成功!");
                        initData();
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"删除失败!");
                    }
                }
            }
        });
    }

    public void initTable(){
        table = new LovoTable(this,
                new String[]{"编号","姓名","电话","性别"},
                new String[]{"id","name","tel","sex"},
                "id");
        table.setSizeAndLocation(30,30,400,200);
    }

    public void initData(){
        List<Teacher> list = teacherDao.list();
        table.updateLovoTable(list);
    }

    public static void main(String[] args) {
        TeacherMainFrame teacherMainFrame = new TeacherMainFrame();
        teacherMainFrame.setVisible(true);
    }
}
